home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / gempp19.zoo / gem++19 / src / gemt.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-01  |  1.8 KB  |  89 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is Copyright 1992,1993 by Warwick W. Allison.
  4. //  This file is part of the gem++ library.
  5. //  You are free to copy and modify these sources, provided you acknowledge
  6. //  the origin by retaining this notice, and adhere to the conditions
  7. //  described in the file COPYING.LIB.
  8. //
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. #include "gemt.h"
  12. #include "gema.h"
  13.  
  14. GEMtimer::GEMtimer(GEMactivity& in, int millisec) :
  15.     interval(millisec), mytime(now+interval)
  16. {
  17.     if (!act) (act=&in)->SetTimer(this);
  18.     InsertInto(head);
  19. }
  20.  
  21. GEMtimer::~GEMtimer()
  22. {
  23.     if (head==this && !next) {
  24.         act->SetTimer(0);
  25.         act=0;
  26.         head=0;
  27.     } else {
  28.         DeleteFrom(head);
  29.     }
  30. }
  31.  
  32. // Below for service provider
  33. int GEMtimer::NextInterval()
  34. {
  35.     if (head)
  36.         return head->mytime-now;
  37.     else
  38.         // WAIT A SEC!  THIS SHOULDN'T HAPPEN!
  39.         return 1000;
  40. }
  41.  
  42. GEMfeedback GEMtimer::ExpireNext(const GEMevent& e)
  43. // Expire() the next GEMtimer and any equal to it.
  44. {
  45.     GEMfeedback result=ContinueInteraction;
  46.     now=head->mytime;
  47.     while (head->mytime <= now && result!=EndInteraction) {
  48.         result=head->Expire(e);
  49.         head->mytime+=head->interval+1; // So interval==0 doesn't lock or starve the scheduler.
  50.         GEMtimer* toinsert=head;
  51.         head=head->next;
  52.         toinsert->InsertInto(head);
  53.     }
  54.     return result;
  55. }
  56.  
  57.  
  58. GEMactivity* GEMtimer::act=0;
  59. GEMtimer* GEMtimer::head=0;
  60. int GEMtimer::now=0;
  61.  
  62. void GEMtimer::InsertInto(GEMtimer*& list)
  63. // Insert self into the given list in order of increasing mytime.
  64. {
  65.     if (list) {
  66.         if (list->mytime > mytime) {
  67.             next=list;
  68.             list=this;
  69.         } else {
  70.             InsertInto(list->next);
  71.         }
  72.     } else {
  73.         next=list;
  74.         list=this;
  75.     }
  76. }
  77.  
  78. void GEMtimer::DeleteFrom(GEMtimer*& list)
  79. // Delete self from the given list
  80. {
  81.     if (list) {
  82.         if (list==this) {
  83.             list=next;
  84.         } else {
  85.             DeleteFrom(list->next);
  86.         }
  87.     }
  88. }
  89.